package com.example.blogapi.service.impl;

import com.example.blogapi.domain.MsCategory;
import com.example.blogapi.mapper.MsCategoryMapper;
import com.example.blogapi.service.CategoryService;
import com.example.blogapi.vo.CategoryVo;
import com.example.blogapi.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    MsCategoryMapper categoryMapper;

    public CategoryVo copy(MsCategory category){
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }
    public List<CategoryVo> copyList(List<MsCategory> categoryList){
        List<CategoryVo> categoryVoList = new ArrayList<>();
        for (MsCategory category : categoryList) {
            categoryVoList.add(copy(category));
        }
        return categoryVoList;
    }
    @Override
    public CategoryVo findCategoryById(Long categoryId) {
        MsCategory category = categoryMapper.selectById(categoryId);
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }

    public Result findAll() {
        List<MsCategory> categories = categoryMapper.selectList(null);
        return Result.success(copyList(categories));
    }
}
